package com.hwz.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwz.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.hwz.gulimall.product.entity.AttrEntity;
import com.hwz.gulimall.product.service.AttrAttrgroupRelationService;
import com.hwz.gulimall.product.service.AttrService;
import com.hwz.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hwz.gulimall.product.entity.AttrGroupEntity;
import com.hwz.gulimall.product.service.AttrGroupService;
import com.hwz.common.utils.PageUtils;
import com.hwz.common.utils.R;



/**
 * 属性分组
 *
 * @author hwz
 * @email 1281561921@qq.com
 * @date 2025-05-27 23:40:51
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{catId}")
    public R listWithKey(@RequestParam Map<String, Object> params,@PathVariable("catId") String catId){
        if (StringUtils.isNotEmpty(catId)){
            params.put("catId",catId);
        }
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //设置catId的完整路径
        attrGroup.setCatIdPath(attrGroupService.getCatIdPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> data = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", data);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrgroupId){
        Page res = attrService.getNoRelationAttr(params, attrgroupId);

        return R.ok().put("page", res);
    }

    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody AttrAttrgroupRelationEntity[] relationEntities){
        attrAttrgroupRelationService.saveBatch(Arrays.asList(relationEntities));

        return R.ok();
    }



    //删除属性组与属性的绑定
    @PostMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrAttrgroupRelationEntity[] relationEntities){
        attrAttrgroupRelationService.batchDelete(Arrays.asList(relationEntities));

        return R.ok();
    }


    //根据分类id查询关联的属性组以及分组下的属性列表
    @RequestMapping("/{cateLogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("cateLogId") String cateLogId){
        List<AttrGroupWithAttrsVo> res = attrGroupService.getAttrGroupWithAttrsByCatId(cateLogId);

        return R.ok().put("data", res);
    }


}
